package eva

import (
	"context"
	"trusteva/app/eva/model"

	"trusteva/app/eva/internal/svc"
	"trusteva/app/eva/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddAppLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddAppLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddAppLogic {
	return &AddAppLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddAppLogic) AddApp(req *types.AddAppRo) (resp *types.AddAppRsp, err error) {
	// TODO 校验权限

	app := &model.App{
		AppName:  req.AppName,
		AppId:    req.AppId,
		DeviceId: req.DeviceId,
		Memo:     req.Memo,
		UserId:   req.UserId,
	}
	err = l.svcCtx.DB.WithContext(l.ctx).Create(app).Error
	if err != nil {
		return nil, err
	}

	err = l.svcCtx.RedisClient.Set(app.Key(), app.Value())
	if err != nil {
		logx.Error("Set", app.Key(), err)
		return nil, err
	}

	return
}
